<template>
  <a-modal
    :open="open"
    width="640px"
    :title="!rowData?.id ? '新增角色' : '修改角色'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex">
      <a-form-item label="角色名称" v-bind="validateInfos.permissonCode" class="w-1/2">
        <a-input v-model:value="formState.permissonCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="顺序号" v-bind="validateInfos.outOrder" class="w-1/2">
        <a-input v-model:value="formState.outOrder" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const formState = ref({
  memo: '',
  permissonCode: '',
  outOrder: '',
  id: ''
})
const rulesState = ref({
  permissonCode: [{ required: true, message: '请输入角色名称' }],
  outOrder: [{ required: true, message: '请输入顺序号' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)

watch(
  () => props.open,
  (val) => {
    if (val && Object.keys(props.rowData).length) {
      formState.value = Object.assign({}, props.rowData)
    }
  }
)
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  await validate()
  const { code, msg, data } = await post('/operation/opex/doSaveRoleInfo', {
    ...formState.value
  })
  if (code === 0) {
    message.success('保存成功')
    emit('success', data)
    onCancel()
  } else {
    message.error(msg)
  }
}
</script>
